//
//  NewFeatureCollectionViewController.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/16.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
private let imageCount = 4

class NewFeatureCollectionViewController: UICollectionViewController {

    //写一个init方法设置collectionView
    init() {
        
        let layout = UICollectionViewFlowLayout()
        //layout设置滑动方向
        layout.scrollDirection = .Horizontal
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        super.init(collectionViewLayout: layout)
        //这里的分页设置需要在super init下面,因为这是父类的方法,需要先初始化父类
        collectionView?.pagingEnabled = true
        
        collectionView?.showsHorizontalScrollIndicator = false
        //取消弹簧效果:bounces
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

             // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
    
        cell.backgroundColor = indexPath.item%2 == 0 ? UIColor.redColor() : UIColor.blackColor()
//        print(indexPath.item)
        
        cell.index = indexPath.item + 1
        
        return cell
    }

    // MARK: UICollectionViewDelegate
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.item)
        
        let cell = collectionView.visibleCells().first! as! NewFeatureCell
        
        let indexP = collectionView.indexPathForCell(cell)
        
        if indexP?.item == imageCount - 1
        {
        cell.showAnimate()
        }
    }

  

}

// MARK: 自定义cell
//直接在一个文件里自定义cell
class NewFeatureCell : UICollectionViewCell {
    
    //定义一个index属性去改变图片
    var index : Int? {
        //当index被赋值时候(OC的set方法)调用didSet{...}
        didSet{
            iconView.image = UIImage(named: String(format: "new_feature_%d", arguments: [index ?? 1]))
            
            finishButton.hidden = index == imageCount ? false : true
            
//            if finishButton.hidden == false
//            {
//            showAnimate()
//            }
        }
    }
    
    //初始化方法(入口)
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        iconView.frame = self.contentView.frame
        
        self.contentView.addSubview(iconView)
        
        self.contentView.addSubview(finishButton)
        
        //添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        finishButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView.snp_centerX)
            make.bottom.equalTo(self.contentView.snp_bottom).offset(-130)
        }
        //为按钮添加方法
        finishButton.addTarget(self, action: "finishStart", forControlEvents: .TouchUpInside)
    }
    
    @objc private func finishStart()
    {
        //新特性
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewController, object: nil)
    }

    //设置动画
    private func showAnimate()
    {
        finishButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 6, options: [], animations: { () -> Void in
            self.finishButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
//                print("OK")
        }
    }

    
    // 懒加载
    private lazy var iconView : UIImageView = {
        
        let _iconView = UIImageView(image: UIImage(named: "new_feature_1"))
        
        return _iconView
    }()
    
    //懒加载按钮
    private lazy var finishButton = UIButton(title: "开始体验", backImageName: "new_feature_finish_button", color: UIColor.whiteColor(), fontSize: 15)
//    private lazy var finishButton : UIButton = {
//       
//        let _finishButton = UIButton()
//        
//        _finishButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
//        _finishButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
//        
//        _finishButton.setTitle("开始体验", forState: .Normal)
//        
//        _finishButton.sizeToFit()
//        
//        return _finishButton
//    }()
}
